﻿using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_EditTicket : Form
    {
        public Form_EditTicket()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();

        private void Init()
        {
            var TicketInfo = (from n in tse.TicketTypes select new { n.TypeID, n.Name, n.Price, n.SpecialPrice, n.Created_at, n.Updated_at }).ToList();
            dGrid_TIckitInfo.DataSource = TicketInfo;
            //修改表头
            dGrid_TIckitInfo.Columns[0].HeaderCell.Value = "ID"; dGrid_TIckitInfo.Columns[1].HeaderCell.Value = "票种"; dGrid_TIckitInfo.Columns[2].HeaderCell.Value = "票价"; dGrid_TIckitInfo.Columns[3].HeaderCell.Value = "特价票"; dGrid_TIckitInfo.Columns[4].HeaderCell.Value = "创建时间"; dGrid_TIckitInfo.Columns[5].HeaderCell.Value = "修改时间";
            txt_OnePrice.Text = TicketInfo[0].Price.ToString();
            txt_TwoPrice.Text = TicketInfo[1].Price.ToString();
            txt_ThreePrice.Text = TicketInfo[2].Price.ToString();
            txt_ParkAdultPrice.Text = TicketInfo[3].Price.ToString();
            txt_ParkSpecialPrice.Text = TicketInfo[3].SpecialPrice.ToString();
        }
        private void Form_EditTicket_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_OnePrice.Text)||string.IsNullOrEmpty(txt_TwoPrice.Text)||string.IsNullOrEmpty(txt_ThreePrice.Text)||string.IsNullOrEmpty(txt_ParkAdultPrice.Text)||string.IsNullOrEmpty(txt_ParkSpecialPrice.Text))
            {
                MessageBox.Show("票价不能为空");
                return;
            }
            try
            {
                Common.Price.PriceOne = Convert.ToDecimal(txt_OnePrice.Text);
                Common.Price.PriceTwo = Convert.ToDecimal(txt_TwoPrice.Text);
                Common.Price.PriceThree = Convert.ToDecimal(txt_ThreePrice.Text);
                Common.Price.PriceParkAdult = Convert.ToDecimal(txt_ParkAdultPrice.Text);
                Common.Price.PriceParkSpecial = Convert.ToDecimal(txt_ParkSpecialPrice.Text);

                var tickets = tse.TicketTypes.ToList();                
                tickets[0].Price = Common.Price.PriceOne;
                tickets[1].Price = Common.Price.PriceTwo;
                tickets[2].Price = Common.Price.PriceThree;
                tickets[3].Price = Common.Price.PriceParkAdult;
                tickets[3].SpecialPrice = Common.Price.PriceParkSpecial;

                tickets[0].Updated_at = DateTime.Now;
                tickets[1].Updated_at = DateTime.Now;
                tickets[2].Updated_at = DateTime.Now;
                tickets[3].Updated_at = DateTime.Now;
                tse.SaveChanges();
                MessageBox.Show("修改成功");
                
                Init();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        //限制只可以输入数字
        private void txt_Number_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("只可以输入数字");
            }
        }
    }
}
